var DEBUG = true;
var NEWS_TYPE = [
	'硬新闻',	
	'深度新闻',
	'普通评论',
	'社论',
	'故事',
];
var PUBLICATION_TYPE = [
	'未发布',
	'已发布',
];
$(document).ready(function() {
	$('#testButton').click(function() {
		getNewsAbstract(1);
	});
	
	//初始最新列表
	getLatest();
	
	//改变新增相册页面
	$('#album-create .photosMore').empty();
	var photos = $('#album-create .photosMore');
	for (i = 0; i < 15; i++)
	{
		var index = i + 1;
		photos.append( $('<div>').addClass('control-group').append($('<label>').addClass('control-label').html('图片' + index))
			.append($('<div>').addClass('controls').append($('<img>').attr({src:"", name:"pic"+i})))
		);
		photos.append( $('<div>').addClass('control-group').append($('<label>').addClass('control-label').html('图片' + index + ' id'))
			.append($('<div>').addClass('controls').append($('<input>').attr({src:"", name:"pic"+i+"id", class:"input-xlarge"})))
		);
		photos.append( $('<div>').addClass('control-group').append($('<label>').addClass('control-label').html('图片' + index + ' 摄影者' ))
			.append($('<div>').addClass('controls').append($('<input>').attr({src:"", name:"pic"+i+"photographer", class:"input-xlarge"})))
		);
		photos.append( $('<div>').addClass('control-group').append($('<label>').addClass('control-label').html('图片' + index + ' 描述'))
			.append($('<div>').addClass('controls').append($('<input>').attr({src:"", name:"pic"+i+"description", class:"input-xlarge"})))
		);
	}
	
	//获得记者列表
	$('.nav-list .showJournalists').live('click', function() {
		getJournalists();
	});
	
	//获得最新三期
	$('.nav-list .showNews').live('click', function() {
		getLatest();
	});
	
	//获得相册列表
	$('.nav-list .showAlbums').live('click', function() {
		albumGetall();
	});
	
	//获得反馈列表
	$('.nav-list .showFeedbacks').live('click', function() {
		feedbackGetall();
	});
	
	//获得评论列表
	$('.nav-list .showComments').live('click', function() {
		commentGetall();
	});
	
	//删除评论
	$('#comments-list .delete').live('click', function() {
		commentDelete($(this).attr('index'));
		return false;
	});
	
	
	//获得资讯列表
	$('.nav-list .showInfos').live('click', function() {
		infoGetall();
	});
	
	//获得某新闻详情
	$('.news-title').live('click', function() {
		$('.nav li.active').removeClass('active');
		//get detail ajax
		var detail = getNewsDetail( $(this).attr('news-id') );
	});
	
	//显示记者详情
	$('.journalist-name').live('click', function() {
		$('.nav li.active').removeClass('active');
		//get detail ajax
		var detail = showJournalistDetail( $(this).attr('index') );
		//bind data
		//if not success, get back
	});
	
	//显示相册详情
	$('.album-name').live('click', function() {
		$('.nav li.active').removeClass('active');
		//get detail ajax
		var detail = albumGet( $(this).attr('album-id') );
	});
	
	//删除资讯
	$('#infos-list .delete').live('click', function() {
		infoDelete($(this).attr('index'));
		return false;
	});
	
	//添加资讯
	$('#info-create').submit(function(){
		infoAdd();
		return false;
	});
	
	//新增相册
	$('#album-create').submit(function(){
		albumAdd();
		return false;
	});
	
	//编辑相册
	$('#album-detail').submit(function(){
		albumEdit($('#album-detail').attr('album-id'));
		return false;
	});
	
	//删除相册
	$('#albums-list .delete').live('click', function() {
		albumDelete($(this).attr('index'));
		return false;
	});
	
	//新增记者
	$('#jour-create').submit(function(){
		journalistAdd();
		return false;
	});
	
	//编辑记者
	$('#journalist-detail').submit(function(){
		journalistEdit($('#journalist-detail').attr('journalist-id'));
		return false;
	});
	
	//删除记者
	$('#journalist-list .delete').live('click', function() {
		journalistDelete($(this).attr('index'));
		return false;
	});
	
	//发布推送
	$('#notification').submit(function(){
		notification();
		return false;
	});
	
	//新增新闻
	$('#news-create').submit(function(){
		newsAdd();
		return false;
	});
	
	//编辑新闻
	$('#news-detail-edit').submit(function() {
		newsEdit($('#news-detail').attr('news-id'));
		return false; //prevent actual submit
	});
	
	//删除新闻
	$('#news-abstract .delete').live('click', function() {
		newsDelete($(this).attr('index'));
		return false;
	});

	//publicate info
	$('#info-create-form').submit(function() {
		publicateInfo();
		return false; //prevent actual submit
	});

	
	$('input[name="date"]').datepicker();
	$('input[name="birthday"]').datepicker();
	$('input[name="start"]').datepicker();
	$('input[name="end"]').datepicker();
	$('input[name="startDate"]').datepicker();
	$('input[name="endDate"]').datepicker();
	
	
	//added by Fangbo Tao
	$('.vote-checkbox').click(function(){
		if($(this).attr("checked"))
			showVoteEditing(true);
		else
			showVoteEditing(false);
	});

	//added by Fangbo Tao
	$('.modal#deleteNews .btn-primary').click(function(){
		deleteNews($('#news-detail').attr('news-id'));
	});
});

//added by Fangbo Tao
function deleteNews(nid)
{
	if (DEBUG) console.log('Call deleteNews, nid = ' + nid);
	var data = {};
	data.nid = nid;
	APIClient('POST', POST_NEWS_DELETION, null, data);
}

//added by Fangbo Tao
function showVoteEditing(show)
{
	if (DEBUG) console.log('Call showVoteEditing');
	if (show)
	{
		var controlDivTitle = $('<div>').addClass('control-group');
		controlDivTitle.append( $('<label>').html('投票问题').attr('class','control-label'));
		var tempDiv = $('<div>').addClass('controls');
		tempDiv.append($('<input>').attr('class','input-xlarge').attr('name', 'question').attr('type','text'));
		controlDivTitle.append(tempDiv);
		
		var controlDivEndDate = $('<div>').addClass('control-group');
		controlDivEndDate.append( $('<label>').html('截止时间').attr('class','control-label'));
		var tempDiv = $('<div>').addClass('controls');
		tempDiv.append($('<input>').attr('class','input-xlarge').attr('name', 'endDate').attr('type','text'));
		controlDivEndDate.append(tempDiv);
		
		var controlDivOptionNum = $('<div>').addClass('control-group');
		controlDivOptionNum.append( $('<label>').html('选项个数').attr('class','control-label'));
		var tempDiv = $('<div>').addClass('controls');
		tempDiv.append($('<input>').attr('class','input-xlarge').attr('name', 'optionNum').attr('type','text'));
		controlDivOptionNum.append(tempDiv);
		
		var controlDivOptions = $('<div>').addClass('control-group');
		for (i = 0; i < 4; i++)
		{
			var index = i+1;
			controlDivOptions.append( $('<label>').html('选项' + index).attr('class','control-label'));
			var tempDiv = $('<div>').addClass('controls');
			tempDiv.append($('<input>').attr('class','input-xlarge').attr('name', 'option' + i).attr('type','text').attr('placeholder','若该选项不需要填写,请留空'));
			controlDivOptions.append(tempDiv);
		}
		
		var voteDiv = $('<div>').addClass('voteDetail');
		voteDiv.append(controlDivTitle);
		voteDiv.append(controlDivEndDate);
		voteDiv.append(controlDivOptionNum);
		voteDiv.append(controlDivOptions);
		$('.controls.vote').append(voteDiv);

		$('input[name="endDate"]').datepicker();
		
	}
	else{
		$('.controls.vote .voteDetail').remove();
	}
}


//added by Fangbo Tao
function publicateInfo(){
	if (DEBUG) console.log('Call publicateInfo');
	var detail = $('#news-detail');
	var data = {};
	data.title = detail.find('input[name="title"]').val();
	data.content = detail.find('textarea[name="content"]').val();
	data.startDate = detail.find('input[name="startDate"]').val();
	data.endDate = detail.find('input[name="endDate"]').val();
	if (DEBUG) console.log(data);
	APIClient('POST', PUBLICATE_INFO, null, data);
}


function getNewsTitle(title, id) {
	var titleDiv = $('<div>').attr({
		'news-id': id,
		'data-toggle': 'tab',
		'data-target': '#news-detail'}).addClass('news-title').html(title);
	return titleDiv;
}

function APIClient(method, APICode, params, mydata) {
	if (APICode >= API_URI.length || APICode < 0) {
		alert('No such API code!');
		return null;
	}
	api_url = HOST_ADDRESS + API_URI[ APICode ];
	if (params != null) {
		var params_encode = '';
		for (key in params) {
			if (params_encode != '') 
				params_encode += '&';
			params_encode += key + '=' + params[key];
		}
		api_url += '?' + params_encode;
	}
	if (DEBUG) console.log('AJAX request url = ' + api_url);
	
	current_api_code = APICode;
	$.ajax({
		type: method,
		url: api_url,
		data: mydata,
		dataType: 'json',
		success:function(data) {
			if (DEBUG) console.log('AJAX return data = ' + data);
			receiveData(current_api_code, data);
		},
		error:function(xhr, ajaxOptions, thrownError){
			switch (xhr.status) {
				case 400:
				case 401:
				case 500:
				default:
					console.error(xhr.status);
					console.error(xhr.responseText);
					console.error(thrownError); 
				break;
			 }
		 } 
	}); 
	//clear old News Abstract
	//TODO: convert return_data to JS object
}